package br.ufpe.cin.imlm2.jbeat.soot.common.filters;

import soot.FastHierarchy;
import soot.Scene;
import soot.SootClass;
import br.ufpe.cin.imlm2.jbeat.soot.common.CommonExceptions;
import br.ufpe.cin.imlm2.util.ArgumentsValidator;

public class CustomExceptionFilter extends ThrowableFilter {

	private SootClass parentException;

	/**
	 * Constructs a new <code>CustomExceptionFilter</code>. If
	 * <code>parentException</code> is null or isn't a a subclass of
	 * java.lang.Throwable an {@link IllegalArgumentException} will be thrown.
	 * 
	 * @param parentException
	 * @throws IllegalArgumentException
	 */
	public CustomExceptionFilter( SootClass parentException ) {
		super();
		ArgumentsValidator.verifyNullness( parentException );
		verifyConstraints( parentException );
		this.parentException = parentException;
	}

	private void verifyConstraints( SootClass parentExc ) {
		FastHierarchy hierarchy = Scene.v().getOrMakeFastHierarchy();
		if ( !hierarchy.isSubclass( parentExc, CommonExceptions.getTHROWABLE() ) )
			throw new IllegalArgumentException( "SootClass '" + parentExc + "' isn't a"
					+ " subclass of " + CommonExceptions.getTHROWABLE() );
	}

	@Override
	public boolean retains( SootClass t ) {
		FastHierarchy hierarchy = Scene.v().getOrMakeFastHierarchy();
		return super.retains( t ) && hierarchy.isSubclass( t, this.parentException );
	}

}
